package com.kdtech.analyse.video;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

/**
 * 毫州试听网 解析类
 * 
 * @author Chase
 * 
 */
public class _3xgdAnalyse implements AnalyseNews {

	

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://news.3xgd.com/html/[0-9\\/]+.html",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}	

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String url = urlMeta.getUrl(); 
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
			System.err.println("不符合规则");
		}

		String html = urlMeta.getHtml();

		/*
		 * 定义新闻实体需要的字段
		 */
		String title = null;
		String desc = null;
		Long dateLong = null;
		String author = null;
		String commentNum = null;
		String clickNum = null;

		Document doc = Jsoup.parse(html);
		
		title = doc.select("div.w680.f_l h2").text();// 标题
		if(StringUtils.isBlank(title))
			{
			title = doc.select("div.w1000.center h1").text();// 标题
			}
		if(StringUtils.isBlank(title))
		{
		title = doc.select("td.title").text();// 标题
		}
		
		dateLong = DateUtils.matchDate(doc.select("div.t_999.t_c").text());// 时间
		if(dateLong==null)
			{
			dateLong = DateUtils.matchDate(doc.select("div.t_ccc").text());// 时间
			}
		if(dateLong==null)
		{
		dateLong = DateUtils.matchDate(doc.select("span.font12").text());// 时间
		}
		
		doc.select("h2.hbxt").remove();
		doc.select("div#vedioplay").remove();
		desc = doc.select("div.news_detail").text();
		if (StringUtils.isBlank(desc)) {
			desc =HtmlCleaner.getContentHtml(url, doc.select("td[colspan=2]"));
		}
		
		author = JSoupUtils.matchAuthor(doc, "来源：");
		NewsMeta meta = new NewsMeta();
		if(StringUtils.isBlank(title)){
		}
		meta.setTitle(title);
		meta.setContent(desc);
		meta.setUrl(url);
		meta.setType(2);
		meta.setClickNum(NumberUtils.parseInt(clickNum));
		meta.setCommentNum(NumberUtils.parseInt(commentNum));
		meta.setDate(dateLong);
		meta.setAuthor(author);

		/**
		 * 解析用于更新的地址
		 */
		String updateUrl= null;
		meta.setUpdateUrl(updateUrl);
		
		return meta;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}


	
}
